import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.InputMismatchException;

/**
 * @author Egor Kulikov (egor@egork.net)
 */
public class TaskD {
	@SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
	InputReader in;
	PrintWriter out;

	void solve() {
		long left = in.readLong();
		long right = in.readLong();
		long beautifulCount = 0;
		beautifulCount = getBeautifulCount(right) - getBeautifulCount(left - 1);
		out.println(beautifulCount);
	}

	private long getBeautifulCount(long maximum) {
		long[][][][][][][] beautifulCount = new long[20][1 << 9][2][9][7][8][2];
		beautifulCount[0][0][0][0][0][0][0] = 1;
		beautifulCount[0][0][1][0][0][0][0] = 1;
		beautifulCount[0][0][0][0][0][0][1] = 1;
		beautifulCount[0][0][1][0][0][0][1] = 1;
		int tenPowerMod7 = 1;
		int tenPowerMod2 = 1;
		for (int i = 1; i < 20; i++) {
			for (int n = 0; n < (1 << 9); n++)
				for (int j = 0; j < 2; j++) {
					for (int k = 0; k < 9; k++) {
						for (int l = 0; l < 7; l++) {
							for (int o = 0; o < 8; o++) {
								for (int p = 0; p < 2; p++) {
									int maxDigit = j == 0 ? 9 : (int) (maximum % 10);
									for (int m = 0; m <= maxDigit; m++) {
										if (m != 0 && ((n >> (m - 1)) & 1) == 0 || i == 1 && ((m % 5 == 0) != (p !=
											0)))
										{
											continue;
										}
										beautifulCount[i][n][j][k][l][o][p] += beautifulCount[i - 1][n][j == 1 && m ==
											maxDigit ? 1 : 0][(k + 9 - m) % 9][(l + 70 - m * tenPowerMod7) % 7][(o +
											80 - m * tenPowerMod2) & 7][p];
										if (m != 0)
											beautifulCount[i][n][j][k][l][o][p] += beautifulCount[i - 1][n - (1 << (m
												- 1))][j == 1 && m == maxDigit ? 1 : 0][(k + 9 - m) % 9][(l + 70 - m
												* tenPowerMod7) % 7][(o + 80 - m * tenPowerMod2) & 7][p];
									}
								}
							}
						}
					}
				}
			maximum /= 10;
			tenPowerMod7 *= 10;
			tenPowerMod7 %= 7;
			tenPowerMod2 *= 10;
			tenPowerMod2 %= 8;
		}
		long totalBeautifulCount = 0;
		for (int k = 0; k < 512; k++) {
			int mask = k * 2;
			int mod9 = 1;
			if (((mask >> 3) & 1) != 0 || ((mask >> 6) & 1) != 0)
				mod9 = 3;
			if (((mask >> 9) & 1) != 0)
				mod9 = 9;
			int mod7 = 1;
			if (((mask >> 7) & 1) != 0)
				mod7 = 7;
			int mod8 = 1;
			if (((mask >> 2) & 1) != 0 || ((mask >> 6) & 1) != 0)
				mod8 = 2;
			if (((mask >> 4) & 1) != 0)
				mod8 = 4;
			if (((mask >> 8) & 1) != 0)
				mod8 = 8;
			int mod5 = 0;
			if (((mask >> 5) & 1) != 0)
				mod5 = 1;
			for (int i = 0; i < 9; i += mod9) {
				for (int j = 0; j < 7; j += mod7) {
					for (int l = 0; l < 8; l += mod8) {
						for (int m = mod5; m < 2; m++)
							totalBeautifulCount += beautifulCount[19][k][1][i][j][l][m];
					}
				}
			}
		}
		return totalBeautifulCount;
	}

	public static void main(String[] args) {
		new TaskD().run();
	}

	TaskD() {
		@SuppressWarnings({"UnusedDeclaration"})
		String id = getClass().getName().toLowerCase();
		//noinspection EmptyTryBlock
		try {
//			System.setIn(new FileInputStream(id + ".in"));
//			System.setOut(new PrintStream(new FileOutputStream(id + ".out")));
//			System.setIn(new FileInputStream("input.txt"));
//			System.setOut(new PrintStream(new FileOutputStream("output.txt")));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		in = new InputReader(System.in);
		out = new PrintWriter(System.out);
	}

	void run() {
		//noinspection InfiniteLoopStatement
		int testCount = in.readInt();
		for (int i = 0; i < testCount; i++)
			solve();
		exit();
	}

	@SuppressWarnings({"UnusedDeclaration"})
	void exit() {
		out.close();
		System.exit(0);
	}

	@SuppressWarnings({"UnusedDeclaration"})
	static class InputReader {
		InputStream stream;
		byte[] buf = new byte[1024];
		int curChar, numChars;

		InputReader(InputStream stream) {
			this.stream = stream;
		}

		int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		int readInt() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		long readLong() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		String readString() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuffer res = new StringBuffer();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		String readLine0() {
			StringBuffer buf = new StringBuffer();
			int c = read();
			while (c != '\n' && c != -1) {
				if (c != '\r')
					buf.appendCodePoint(c);
				c = read();
			}
			return buf.toString();
		}

		String readLine() {
			String s = readLine0();
			while (s.trim().length() == 0)
				s = readLine0();
			return s;
		}

		String readLine(boolean ignoreEmptyLines) {
			if (ignoreEmptyLines)
				return readLine();
			else
				return readLine0();
		}

		BigInteger readBigInteger() {
			try {
				return new BigInteger(readString());
			} catch (NumberFormatException e) {
				throw new InputMismatchException();
			}
		}

		char readCharacter() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			return (char) c;
		}

		double readDouble() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			double res = 0;
			while (!isSpaceChar(c) && c != '.') {
				if (c == 'e' || c == 'E')
					return res * Math.pow(10, readInt());
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			if (c == '.') {
				c = read();
				double m = 1;
				while (!isSpaceChar(c)) {
					if (c == 'e' || c == 'E')
						return res * Math.pow(10, readInt());
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					m /= 10;
					res += (c - '0') * m;
					c = read();
				}
			}
			return res * sgn;
		}
	}
}
